/*
 * Copyright (c) 2019 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.Image;
import ohos.app.Context;

import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.internal.IconicsViewsAttrsApplier;

/**
 * 设置字体图标的Image
 *
 * @since 2017-06-07
 */
public class IconicsImageView extends Image implements Component.ComponentStateChangedListener {
    private IconicsDrawable icon;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public IconicsImageView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性
     * @param styleName 样式
     */
    public IconicsImageView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    /**
     * 获取图标
     *
     * @return IconicsDrawable
     */
    public IconicsDrawable getIcon() {
        return icon;
    }

    private void init(Context context, AttrSet attrSet) {
        setScaleMode(ScaleMode.INSIDE);
        setComponentStateChangedListener(this);
        icon = IconicsViewsAttrsApplier.getIconicsImageViewDrawable(this, context, attrSet);
        if (icon != null) {
            setIcon(icon);
        }
    }

    /**
     * 设置图标
     *
     * @param icon 图标
     */
    public void setIcon(IconicsDrawable icon) {
        this.icon = icon;
        final int unSize = 2147483647;
        if (this.icon.getBounds().getWidth() == unSize || this.icon.getBounds().getHeight() == unSize) {
            this.icon.setBounds(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom());
        } else {
            int left = (getWidth() - this.icon.getBounds().getWidth()) / 2 + getPaddingLeft();
            int top = (getHeight() - this.icon.getBounds().getHeight()) / 2 + getPaddingTop();
            this.icon.setBounds(left, top, left + this.icon.getBounds().getWidth() - getPaddingRight(),
                top + this.icon.getBounds().getHeight() - getPaddingBottom());
        }
        addDrawTask(icon);
        invalidate();
    }

    @Override
    public void onComponentStateChanged(Component component, int st) {
        if (icon != null) {
            int state;
            if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED, st)) {
                state = ComponentState.COMPONENT_STATE_PRESSED;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_HOVERED, st)) {
                state = ComponentState.COMPONENT_STATE_HOVERED;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_EMPTY, st)) {
                state = ComponentState.COMPONENT_STATE_EMPTY;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_CHECKED, st)) {
                state = ComponentState.COMPONENT_STATE_CHECKED;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_DISABLED, st)) {
                state = ComponentState.COMPONENT_STATE_DISABLED;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_FOCUSED, st)) {
                state = ComponentState.COMPONENT_STATE_FOCUSED;
            } else if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_SELECTED, st)) {
                state = ComponentState.COMPONENT_STATE_SELECTED;
            } else {
                state = ComponentState.COMPONENT_STATE_EMPTY;
            }
            icon.setState(state);
            invalidate();
        }
    }
}
